<template>
  <div class="app-container">
    <el-form ref="filterForm" :inline="true" :model="listQuery" class="filter-container">
      <el-form-item prop="id">
        <el-input v-model="listQuery.id" size="small" placeholder="ID" clearable style="width: 100px;" @keyup.enter.native="handleFilter" />
      </el-form-item>
      <el-form-item prop="name">
        <el-input v-model="listQuery.name" size="small" placeholder="名称" clearable style="width: 200px;" @keyup.enter.native="handleFilter" />
      </el-form-item>

      <el-form-item prop="typeid">
        <el-select v-model="listQuery.typeid" size="small" placeholder="类型" clearable style="width: 100px" @change="handleFilter">
          <el-option v-for="item in typeOptions" :key="item.id" :label="item.name" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item prop="online">
        <el-select v-model="listQuery.online" size="small" placeholder="是否上线" clearable style="width: 110px" @change="handleFilter">
          <el-option v-for="item in onlineOptions" :key="item.key" :label="item.label" :value="item.key" />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button size="small" type="primary" icon="el-icon-search" @click="handleFilter">筛选</el-button>
        <el-button size="small" @click="resetFilter">重置</el-button>
        <el-button
          size="small"
          type="success"
          icon="el-icon-plus"
          @click="toCategoryNew"
        >添加分类</el-button>
      </el-form-item>
    </el-form>

    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      stripe
      fit
      highlight-current-row
    >
      <el-table-column label="ID" width="80" align="center">
        <template slot-scope="scope">
          {{ scope.row.id }}
        </template>
      </el-table-column>
      <el-table-column label="位置" width="80" align="center">
        <template slot-scope="scope">
          {{ scope.row.place }}
        </template>
      </el-table-column>
      <el-table-column label="名称">
        <template slot-scope="scope">
          {{ scope.row.name }}
        </template>
      </el-table-column>
      <el-table-column label="图标" align="center">
        <template slot-scope="scope">
          <img :src="scope.row.icon" alt="" width="120">
        </template>
      </el-table-column>
      <el-table-column label="上级分类" align="center">
        <template slot-scope="scope">
          {{ scope.row.typename }}
        </template>
      </el-table-column>
      <el-table-column label="上线" width="80" align="center">
        <template slot-scope="scope">
          <i v-if="scope.row.online === 1" class="el-icon-check text-success" />
          <i v-else class="el-icon-close text-warning" />
        </template>
      </el-table-column>
      <el-table-column label="操作" width="120" align="center">
        <template slot-scope="scope">
          <el-dropdown trigger="click">
            <el-button
              size="small"
              plain
            >操作 <i class="el-icon-arrow-down el-icon--right" /></el-button>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item
                @click.native="toCategoryEdit(scope.row.id)"
              >编辑</el-dropdown-item>
              <el-dropdown-item
                divided
                @click.native="setTop(scope.row.id)"
              >置顶</el-dropdown-item>
              <el-dropdown-item
                @click.native="moveUp(scope.row.id)"
              >上移</el-dropdown-item>
              <el-dropdown-item
                @click.native="moveDown(scope.row.id)"
              >下移</el-dropdown-item>
              <el-dropdown-item
                @click.native="setBottom(scope.row.id)"
              >置底</el-dropdown-item>
              <el-dropdown-item
                divided
                @click.native="removeCategory(scope.row.id)"
              >
                <span class="text-danger">删除</span>
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import {
  getCategories,
  removeCategory,
  categorySetTop,
  categorySetBottom,
  categoryMoveUp,
  categoryMoveDown
} from '@/api/articles'
import {
  getArticleTypes
} from '@/api/baseData'
import {
  formatQuery
} from '@/utils'

export default {
  name: 'ArticlesCategoryList',
  data() {
    return {
      list: null,
      listLoading: true,
      listQuery: {
        id: formatQuery(this.$route.query.id, false),
        name: formatQuery(this.$route.query.name, false),
        typeid: formatQuery(this.$route.query.typeid, true),
        online: formatQuery(this.$route.query.online, true)
      },
      typeOptions: [],
      onlineOptions: [
        {
          label: '是',
          key: 1
        },
        {
          label: '否',
          key: 0
        }
      ]
    }
  },
  created() {
    this.fetchArticleTypes()
    this.fetchData()
  },
  methods: {
    fetchArticleTypes() {
      getArticleTypes().then(response => {
        this.typeOptions = response.data
      }).catch(err => {
        console.log(err)
      })
    },
    fetchData() {
      this.listLoading = true
      const query = {
        ...this.$route.query,
        ...this.listQuery
      }
      this.$router.push({
        query
      }).catch(err => {
        console.log(err)
      })
      getCategories(this.listQuery).then(response => {
        // console.log(response)
        this.list = response.data
        this.listLoading = false
      }).catch(err => {
        console.log(err)
      })
    },
    removeCategory(id) {
      this.$confirm('此操作将永久删除该分类, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        removeCategory({ id }).then(response => {
          // console.log(response)
          this.handleFilter()
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        }).catch(err => {
          console.log(err)
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    handleFilter() {
      this.list = []
      this.fetchData()
    },
    resetFilter() {
      this.$refs.filterForm.resetFields()
      for (const i in this.listQuery) {
        if (i !== 'size') {
          this.listQuery[i] = undefined
        }
      }
      // console.log(this.listQuery)
      this.handleFilter()
    },
    toCategoryNew() {
      this.$router.push('/articles-category/new')
    },
    toCategoryEdit(id) {
      this.$router.push(`/articles-category/edit/${id}`)
    },
    setTop(id) {
      categorySetTop({ id }).then(response => {
        // console.log(response)
        if (response.rtncode === 200) {
          this.$message({
            type: 'success',
            message: '操作成功!'
          })
          this.handleFilter()
        } else {
          this.$message({
            type: 'error',
            message: '操作失败，请稍后再试!'
          })
        }
      }).catch(err => {
        console.log(err)
      })
    },
    setBottom(id) {
      categorySetBottom({ id }).then(response => {
        // console.log(response)
        if (response.rtncode === 200) {
          this.$message({
            type: 'success',
            message: '操作成功!'
          })
          this.handleFilter()
        } else {
          this.$message({
            type: 'error',
            message: '操作失败，请稍后再试!'
          })
        }
      }).catch(err => {
        console.log(err)
      })
    },
    moveUp(id) {
      categoryMoveUp({ id }).then(response => {
        // console.log(response)
        if (response.rtncode === 200) {
          this.$message({
            type: 'success',
            message: '操作成功!'
          })
          this.handleFilter()
        } else {
          this.$message({
            type: 'error',
            message: '操作失败，请稍后再试!'
          })
        }
      }).catch(err => {
        console.log(err)
      })
    },
    moveDown(id) {
      categoryMoveDown({ id }).then(response => {
        // console.log(response)
        if (response.rtncode === 200) {
          this.$message({
            type: 'success',
            message: '操作成功!'
          })
          this.handleFilter()
        } else {
          this.$message({
            type: 'error',
            message: '操作失败，请稍后再试!'
          })
        }
      }).catch(err => {
        console.log(err)
      })
    }
  }
}
</script>
